<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\Admin\NoticeRequest;
use App\Services\Admin\NoticeService;

class NoticeController extends Controller
{
    /**
     * 全部标记为已读
     */
    public function readAll()
    {
        $data = NoticeService::readAll();
        return $this->success(msg: '全部公告已标记为已读', data: $data);
    }


    /**
     * 获取公告并标记公告为已读
     */
    public function read(string $id)
    {
        $data = NoticeService::read($id);
        return $this->success(data: $data);
    }


    /**
     * 我的公告分页列表
     */
    public function myPage(Request $request)
    {
        $params = $request->validate([
            'isRead'   => 'nullable|in:0,1',
            'pageNum'  => 'nullable|integer|min:1',
            'pageSize' => 'nullable|integer|min:5',
        ]);

        $params['pageNum'] = $params['pageNum'] ?? 1;
        $params['pageSize'] = $params['pageSize'] ?? 5;

        $data = NoticeService::myPage($params);

        return $this->success(data: $data);
    }



    /**
     * 撤回公告
     */
    public function revoke(string $id)
    {
        $data = NoticeService::revoke($id);

        return $this->success(msg: '公告已撤回', data: $data);
    }



    /**
     * 发布公告
     */
    public function publish(string $id)
    {
        $data = NoticeService::publish($id);

        return $this->success(msg: '公告已发布', data: $data);
    }

    /**
     * 显示资源列表。
     */
    public function index(Request $request)
    {
        $params = $request->validate([
            'pageNum'      => 'nullable|integer|min:1',
            'pageSize'     => 'nullable|integer|min:5',
        ]);

        $params['pageNum'] = $params['pageNum'] ?? 1;
        $params['pageSize'] = $params['pageSize'] ?? 10;

        $data = NoticeService::index($params);

        return $this->success(data: $data);
    }

    /**
     * 将新创建的资源存储到数据库。
     */
    public function store(NoticeRequest $request)
    {
        $validated = $request->validated();

        $data = NoticeService::store($validated);

        return $this->success(data: $data);
    }

    /**
     * 显示指定资源的详细信息。
     */
    public function show(string $id)
    {

        $data = NoticeService::show($id);

        return $this->success(data: $data);
    }

    /**
     * 更新指定资源。
     */
    public function update(NoticeRequest $request, string $id)
    {
        $validated = $request->validated();

        $data = NoticeService::update($validated, $id);

        return $this->success(data: $data);
    }

    /**
     * 删除指定资源。
     */
    public function destroy(string $id)
    {
        $data = NoticeService::destroy($id);

        return $this->success(data: $data);
    }
}
